home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume30 / log_tcp / part02 < prev    next >
Encoding:
Text File  |  1992-06-20  |  43.0 KB  |  1,407 lines

  1. Newsgroups: comp.sources.misc
  2. From: wietse@wzv.win.tue.nl (Wietse Venema)
  3. Subject:  v30i080:  log_tcp - TCP/IP daemon wrapper, Part02/02
  4. Message-ID: <1992Jun21.042647.4406@sparky.imd.sterling.com>
  5. X-Md4-Signature: 05a4d3a7a6d5f28397ec6db8056f3377
  6. Date: Sun, 21 Jun 1992 04:26:47 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: wietse@wzv.win.tue.nl (Wietse Venema)
  10. Posting-number: Volume 30, Issue 80
  11. Archive-name: log_tcp/part02
  12. Environment: UNIX
  13. Supersedes: log_tcp: Volume 23, Issue 77
  14.  
  15. #! /bin/sh
  16. # This is a shell archive.  Remove anything before this line, then feed it
  17. # into a shell via "sh file" or similar.  To overwrite existing files,
  18. # type "sh file -c".
  19. # The tool that generated this appeared in the comp.sources.unix newsgroup;
  20. # send mail to comp-sources-unix@uunet.uu.net if you want that tool.
  21. # Contents:  BLURB clean_exit.c hosts_access.3 hosts_ctl.c hosts_info.c
  22. #   log_tcp.h miscd.c percent_x.c refuse.c rfc931.c rfc931_option
  23. #   shell_cmd.c strcasecmp.c tcpd.8 tcpd.c try.c
  24. # Wrapped by kent@sparky on Sat Jun 20 23:22:24 1992
  25. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  26. echo If this archive is complete, you will see the following message:
  27. echo '          "shar: End of archive 2 (of 2)."'
  28. if test -f 'BLURB' -a "${1}" != "-c" ; then 
  29.   echo shar: Will not clobber existing file \"'BLURB'\"
  30. else
  31.   echo shar: Extracting \"'BLURB'\" \(1635 characters\)
  32.   sed "s/^X//" >'BLURB' <<'END_OF_FILE'
  33. X@(#) BLURB 1.5 92/06/11 22:21:40
  34. X
  35. XThis package provides a couple of tiny programs that monitor incoming
  36. Xrequests for IP services such as TFTP, EXEC, FTP, RSH, TELNET, RLOGIN,
  37. XFINGER, SYSTAT, and many others.
  38. X
  39. XOptional features are: access control based on pattern matching; remote
  40. Xusername lookup using the RFC 931 protocol; protection against rsh and
  41. Xrlogin attacks from hosts that pretend to have someone elses name.
  42. X
  43. XThe programs can be installed without requiring any changes to existing
  44. Xsoftware or configuration files. By default, they just log the remote
  45. Xhost name and then invoke the real network daemon. No information is
  46. Xexchanged with the remote client process.
  47. X
  48. XEnhancements over the previous release are: 
  49. X
  50. X    1 - network daemons no longer have to live within a common directory
  51. X    2 - the access control code now uses both the host address and name
  52. X    3 - an access control pattern that supports netmasks
  53. X    4 - additional protection against forged host names
  54. X    5 - a pattern that matches hosts whose name or address lookup fails
  55. X    6 - an operator that prevents hosts or services from being matched
  56. X    7 - optional remote username lookup with the RFC 931 protocol
  57. X    8 - an optional umask to prevent the creation of world-writable files
  58. X    9 - hooks for access control language extensions
  59. X   10 - last but not least, thoroughly revised documentation.
  60. X
  61. XExcept for the change described under (2) the present version should be
  62. Xbackwards compatible with earlier ones.
  63. X
  64. X    Wietse Venema (wietse@wzv.win.tue.nl),
  65. X    Department of Mathematics and Computing Science,
  66. X    Eindhoven University of Technology,
  67. X    The Netherlands.
  68. END_OF_FILE
  69.   if test 1635 -ne `wc -c <'BLURB'`; then
  70.     echo shar: \"'BLURB'\" unpacked with wrong size!
  71.   fi
  72.   # end of 'BLURB'
  73. fi
  74. if test -f 'clean_exit.c' -a "${1}" != "-c" ; then 
  75.   echo shar: Will not clobber existing file \"'clean_exit.c'\"
  76. else
  77.   echo shar: Extracting \"'clean_exit.c'\" \(1268 characters\)
  78.   sed "s/^X//" >'clean_exit.c' <<'END_OF_FILE'
  79. X /*
  80. X  * clean_exit() cleans up and terminates the program. It should be called
  81. X  * instead of exit when for some reason the real network daemon will not or
  82. X  * cannot be run. Reason: in the case of a datagram-oriented service we must
  83. X  * discard the not-yet received data from the client. Otherwise, inetd will
  84. X  * see the same datagram again and again, and go into a loop.
  85. X  * 
  86. X  * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
  87. X  */
  88. X
  89. X#ifndef lint
  90. Xstatic char sccsid[] = "@(#) clean_exit.c 1.1 92/06/11 22:21:52";
  91. X#endif
  92. X
  93. X#include <sys/types.h>
  94. X#include <sys/socket.h>
  95. X#include <stdio.h>
  96. X
  97. Xextern void exit();
  98. X
  99. X#include "log_tcp.h"
  100. X
  101. X/* clean_exit - clean up and exit */
  102. X
  103. Xvoid    clean_exit(client)
  104. Xstruct from_host *client;
  105. X{
  106. X    char    buf[BUFSIZ];
  107. X    struct sockaddr sa;
  108. X    int     size = sizeof(sa);
  109. X
  110. X    /*
  111. X     * Eat up the not-yet received packet. Some systems insist on a non-zero
  112. X     * source address argument in the recvfrom() call below.
  113. X     */
  114. X
  115. X    if (client->sock_type == FROM_UNCONNECTED)
  116. X    (void) recvfrom(0, buf, sizeof(buf), 0, &sa, &size);
  117. X
  118. X    /*
  119. X     * Be kind to the inetd. We already reported the problem via the syslogd,
  120. X     * and there is no need for additional garbage in the logfile.
  121. X     */
  122. X
  123. X    exit(0);
  124. X}
  125. END_OF_FILE
  126.   if test 1268 -ne `wc -c <'clean_exit.c'`; then
  127.     echo shar: \"'clean_exit.c'\" unpacked with wrong size!
  128.   fi
  129.   # end of 'clean_exit.c'
  130. fi
  131. if test -f 'hosts_access.3' -a "${1}" != "-c" ; then 
  132.   echo shar: Will not clobber existing file \"'hosts_access.3'\"
  133. else
  134.   echo shar: Extracting \"'hosts_access.3'\" \(2018 characters\)
  135.   sed "s/^X//" >'hosts_access.3' <<'END_OF_FILE'
  136. X.TH HOSTS_ACCESS 3
  137. X.SH
  138. Xhosts_access, hosts_ctl \- access control library
  139. X.SH SYNOPSIS
  140. X.nf
  141. X#include "log_tcp.h"
  142. X
  143. Xint hosts_access(daemon, client)
  144. Xchar *daemon;
  145. Xstruct from_host *client;
  146. X
  147. Xint hosts_ctl(daemon, client_name, client_addr, client_user)
  148. Xchar *daemon;
  149. Xchar *client_host;
  150. Xchar *client_addr;
  151. Xchar *client_user;
  152. X.fi
  153. X.SH DESCRIPTION
  154. XThe routines described in this document are part of the \fIlibwrap.a\fR
  155. Xlibrary. They implement a pattern-based access control language with
  156. Xoptional shell commands that are executed when a pattern fires.
  157. X.PP
  158. XIn all cases, the daemon argument should specify a daemon process name
  159. X(argv[0] value).  The client host address should be a valid address, or
  160. XFROM_UNKNOWN if address lookup failed. The client host name and user
  161. Xname should be empty strings if no information is available,
  162. XFROM_UNKNOWN if lookup failed, or an actual host or user name.
  163. X.PP
  164. Xhosts_access() consults the access control tables described in the
  165. X\fIhosts_access(5)\fR manual page.  If a match is found, an optional
  166. Xshell command is executed and the search terminates. hosts_access()
  167. Xreturns zero if access should be denied.
  168. X.PP
  169. Xhosts_ctl() is a wrapper around the hosts_access() routine with a
  170. Xperhaps more convenient interface.  hosts_ctl() returns zero if access
  171. Xshould be denied.
  172. X.SH DIAGNOSTICS
  173. XProblems are reported via the syslog daemon.
  174. X.SH SEE ALSO
  175. Xhosts_access(5), format of the access control tables.
  176. X.SH FILES
  177. X/etc/hosts.access, /etc/hosts.deny, access control tables.
  178. X.SH BUGS
  179. XThe functions described here do not make copies of their string-valued
  180. Xarguments.  Beware of data from functions that overwrite their results
  181. Xupon each call.
  182. X.sp
  183. Xhosts_access() uses the strtok() library function. This may interfere
  184. Xwith other code that relies on strtok().
  185. X.SH AUTHOR
  186. X.na
  187. X.nf
  188. XWietse Venema (wietse@wzv.win.tue.nl)
  189. XDepartment of Mathematics and Computing Science
  190. XEindhoven University of Technology
  191. XDen Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
  192. X\" @(#) hosts_access.3 1.1 92/06/11 22:21:45
  193. END_OF_FILE
  194.   if test 2018 -ne `wc -c <'hosts_access.3'`; then
  195.     echo shar: \"'hosts_access.3'\" unpacked with wrong size!
  196.   fi
  197.   # end of 'hosts_access.3'
  198. fi
  199. if test -f 'hosts_ctl.c' -a "${1}" != "-c" ; then 
  200.   echo shar: Will not clobber existing file \"'hosts_ctl.c'\"
  201. else
  202.   echo shar: Extracting \"'hosts_ctl.c'\" \(969 characters\)
  203.   sed "s/^X//" >'hosts_ctl.c' <<'END_OF_FILE'
  204. X /*
  205. X  * hosts_ctl() combines the most common applications of the host access
  206. X  * control library. routine. It bundles its arguments into a from_host
  207. X  * structure, then calls the hosts_access() access control checker. The host
  208. X  * name and user name arguments should be empty strings, "unknown" or real
  209. X  * data. if a match is found, the optional shell command is executed.
  210. X  * 
  211. X  * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
  212. X  */
  213. X
  214. X#ifndef lint
  215. Xstatic char sccsid[] = "@(#) hosts_ctl.c 1.1 92/06/11 22:21:48";
  216. X#endif
  217. X
  218. X#include <stdio.h>
  219. X
  220. X#include "log_tcp.h"
  221. X
  222. X/* hosts_ctl - general interface for the hosts_access() routine */
  223. X
  224. Xint     hosts_ctl(daemon, name, addr, user)
  225. Xchar   *daemon;
  226. Xchar   *name;
  227. Xchar   *addr;
  228. Xchar   *user;
  229. X{
  230. X    struct from_host client;
  231. X    static struct from_host zeros;
  232. X
  233. X    client = zeros;
  234. X    client.name = name;
  235. X    client.addr = addr;
  236. X    client.user = user;
  237. X
  238. X    return (hosts_access(daemon, &client));
  239. X}
  240. END_OF_FILE
  241.   if test 969 -ne `wc -c <'hosts_ctl.c'`; then
  242.     echo shar: \"'hosts_ctl.c'\" unpacked with wrong size!
  243.   fi
  244.   # end of 'hosts_ctl.c'
  245. fi
  246. if test -f 'hosts_info.c' -a "${1}" != "-c" ; then 
  247.   echo shar: Will not clobber existing file \"'hosts_info.c'\"
  248. else
  249.   echo shar: Extracting \"'hosts_info.c'\" \(788 characters\)
  250.   sed "s/^X//" >'hosts_info.c' <<'END_OF_FILE'
  251. X /*
  252. X  * hosts_info() returns a string with as much information about the origin
  253. X  * of a connection as we have: the user name, if known, and the host name,
  254. X  * or the host address if the name is not available.
  255. X  * 
  256. X  * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
  257. X  */
  258. X
  259. X#ifndef lint
  260. Xstatic char sccsid[] = "@(#) hosts_info.c 1.1 92/06/11 22:21:44";
  261. X#endif
  262. X
  263. X#include <stdio.h>
  264. X
  265. X#include "log_tcp.h"
  266. X
  267. X/* hosts_info - return string with as much about the client as we know */
  268. X
  269. Xchar   *hosts_info(client)
  270. Xstruct from_host *client;
  271. X{
  272. X    static char buf[BUFSIZ];        /* XXX */
  273. X
  274. X    if (client->user[0] && strcmp(client->user, FROM_UNKNOWN)) {
  275. X    sprintf(buf, "%s@%s", client->user, FROM_HOST(client));
  276. X    return (buf);
  277. X    } else {
  278. X    return (FROM_HOST(client));
  279. X    }
  280. X}
  281. END_OF_FILE
  282.   if test 788 -ne `wc -c <'hosts_info.c'`; then
  283.     echo shar: \"'hosts_info.c'\" unpacked with wrong size!
  284.   fi
  285.   # end of 'hosts_info.c'
  286. fi
  287. if test -f 'log_tcp.h' -a "${1}" != "-c" ; then 
  288.   echo shar: Will not clobber existing file \"'log_tcp.h'\"
  289. else
  290.   echo shar: Extracting \"'log_tcp.h'\" \(1400 characters\)
  291.   sed "s/^X//" >'log_tcp.h' <<'END_OF_FILE'
  292. X/* @(#) log_tcp.h 1.2 92/06/11 22:21:30 */
  293. X
  294. X/* Location of the access control files. */
  295. X
  296. X#define HOSTS_ALLOW    "/etc/hosts.allow"
  297. X#define HOSTS_DENY    "/etc/hosts.deny"
  298. X
  299. X /*
  300. X  * Structure filled in by the fromhost() routine. Prerequisites:
  301. X  * <sys/types.h> and <sys/param.h>.
  302. X  */
  303. X
  304. X#ifndef MAXHOSTNAMELEN
  305. X#define MAXHOSTNAMELEN    1024        /* string with host name */
  306. X#endif
  307. X
  308. X#define FROM_ADDRLEN    (4*3+3+1)    /* string with IP address */
  309. X
  310. Xstruct from_host {
  311. X    int     sock_type;            /* socket type, see below */
  312. X    char   *name;            /* host name */
  313. X    char   *addr;            /* host address */
  314. X    char   *user;            /* user name */
  315. X};
  316. X
  317. X#define FROM_UNKNOWN    "unknown"    /* name or address lookup failed */
  318. X#define FROM_HOST(f) \
  319. X    (((f)->name[0] && strcmp((f)->name, FROM_UNKNOWN)) ? (f)->name : (f)->addr)
  320. X
  321. X/* Socket types: 0 means unknown. */
  322. X
  323. X#define FROM_CONNECTED        1    /* connection-oriented */
  324. X#define FROM_UNCONNECTED    2    /* non connection-oriented */
  325. X
  326. X/* Global functions. */
  327. X
  328. Xextern int fromhost();            /* get/validate remote host info */
  329. Xextern int hosts_access();        /* access control */
  330. Xextern void refuse();            /* refuse request */
  331. Xextern void shell_cmd();        /* execute shell command */
  332. Xextern void percent_x();        /* do %<char> expansion */
  333. Xextern char *rfc931_name();        /* remote name from RFC 931 daemon */
  334. Xextern char *hosts_info();        /* show origin of connection */
  335. Xextern void clean_exit();        /* clean up and exit */
  336. END_OF_FILE
  337.   if test 1400 -ne `wc -c <'log_tcp.h'`; then
  338.     echo shar: \"'log_tcp.h'\" unpacked with wrong size!
  339.   fi
  340.   # end of 'log_tcp.h'
  341. fi
  342. if test -f 'miscd.c' -a "${1}" != "-c" ; then 
  343.   echo shar: Will not clobber existing file \"'miscd.c'\"
  344. else
  345.   echo shar: Extracting \"'miscd.c'\" \(2761 characters\)
  346.   sed "s/^X//" >'miscd.c' <<'END_OF_FILE'
  347. X /*
  348. X  * Front end to the ULTRIX miscd service. The front end logs the remote host
  349. X  * name and then invokes the real miscd daemon. Install as "/usr/etc/miscd",
  350. X  * after moving the real miscd daemon to the "/usr/etc/..." directory.
  351. X  * Connections and diagnostics are logged through syslog(3).
  352. X  * 
  353. X  * The Ultrix miscd program implements (among others) the systat service, which
  354. X  * pipes the output from who(1) to stdout. This information is potentially
  355. X  * useful to systems crackers.
  356. X  * 
  357. X  * Compile with -DHOSTS_ACCESS in order to enable access control. See the
  358. X  * hosts_access(5) manual page for details.
  359. X  * 
  360. X  * Compile with -DPARANOID if service should be refused to hosts that pretend
  361. X  * to have someone elses host name. This gives some protection against rsh
  362. X  * and rlogin attacks that involve compromised domain name servers.
  363. X  * 
  364. X  * Compile with -DDAEMON_UMASK=nnn if daemons should run with a non-default
  365. X  * umask value (the system default is 000, resulting in world-writable
  366. X  * files).
  367. X  * 
  368. X  * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
  369. X  */
  370. X
  371. X#ifndef lint
  372. Xstatic char sccsid[] = "@(#) miscd.c 1.3 92/06/11 22:21:20";
  373. X#endif
  374. X
  375. X/* System libraries. */
  376. X
  377. X#include <sys/types.h>
  378. X#include <sys/param.h>
  379. X#include <sys/stat.h>
  380. X#include <stdio.h>
  381. X#include <syslog.h>
  382. X
  383. X/* Local stuff. */
  384. X
  385. X#include "log_tcp.h"
  386. X
  387. X/* The following specifies where the vendor-provided daemon should go. */
  388. X
  389. X#define REAL_DAEMON    "/usr/etc/.../miscd"
  390. X
  391. Xmain(argc, argv)
  392. Xint     argc;
  393. Xchar  **argv;
  394. X{
  395. X    struct from_host from;
  396. X    int     from_stat;
  397. X
  398. X    /* Attempt to prevent the creation of world-writable files. */
  399. X
  400. X#ifdef DAEMON_UMASK
  401. X    umask(DAEMON_UMASK);
  402. X#endif
  403. X
  404. X    /*
  405. X     * Open a channel to the syslog daemon. Older versions of openlog()
  406. X     * require only two arguments.
  407. X     */
  408. X
  409. X#ifdef LOG_MAIL
  410. X    (void) openlog(argv[0], LOG_PID, FACILITY);
  411. X#else
  412. X    (void) openlog(argv[0], LOG_PID);
  413. X#endif
  414. X
  415. X    /*
  416. X     * Find out and verify the remote host name. Sites concerned with
  417. X     * security may choose to refuse connections from hosts that pretend to
  418. X     * have someone elses host name.
  419. X     */
  420. X
  421. X    from_stat = fromhost(&from);
  422. X#ifdef PARANOID
  423. X    if (from_stat == -1)
  424. X    refuse(&from);
  425. X#endif
  426. X
  427. X    /*
  428. X     * Check whether this host can access the service in argv[0]. The
  429. X     * access-control code invokes optional shell commands as specified in
  430. X     * the access-control tables.
  431. X     */
  432. X
  433. X#ifdef HOSTS_ACCESS
  434. X    if (!hosts_access(argv[0], &from))
  435. X    refuse(&from);
  436. X#endif
  437. X
  438. X    /* Report remote client and invoke the real daemon program. */
  439. X
  440. X    syslog(LOG_INFO, "connect from %s", hosts_info(&from));
  441. X    (void) execv(REAL_DAEMON, argv);
  442. X    syslog(LOG_ERR, "%s: %m", REAL_DAEMON);
  443. X    clean_exit(&from);
  444. X    /* NOTREACHED */
  445. X}
  446. END_OF_FILE
  447.   if test 2761 -ne `wc -c <'miscd.c'`; then
  448.     echo shar: \"'miscd.c'\" unpacked with wrong size!
  449.   fi
  450.   # end of 'miscd.c'
  451. fi
  452. if test -f 'percent_x.c' -a "${1}" != "-c" ; then 
  453.   echo shar: Will not clobber existing file \"'percent_x.c'\"
  454. else
  455.   echo shar: Extracting \"'percent_x.c'\" \(2134 characters\)
  456.   sed "s/^X//" >'percent_x.c' <<'END_OF_FILE'
  457. X /*
  458. X  * percent_x() takes a string and performs %a (host address), %c (client
  459. X  * info), %h (host name or address), %d (daemon name), %p (process id) and
  460. X  * %u (user name) substitutions. It aborts the program when the result of
  461. X  * expansion would overflow the output buffer.
  462. X  * 
  463. X  * Diagnostics are reported through syslog(3).
  464. X  * 
  465. X  * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
  466. X  */
  467. X
  468. X#ifndef lint
  469. Xstatic char sccsid[] = "@(#) percent_x.c 1.1 92/06/11 22:21:49";
  470. X#endif
  471. X
  472. X/* System libraries. */
  473. X
  474. X#include <stdio.h>
  475. X#include <syslog.h>
  476. X
  477. Xextern char *strncpy();
  478. Xextern void exit();
  479. X
  480. X/* Local stuff. */
  481. X
  482. X#include "log_tcp.h"
  483. X
  484. X/* percent_x - do %<char> expansion, abort if result buffer is too small */
  485. X
  486. Xvoid    percent_x(result, result_len, str, daemon, client, pid)
  487. Xchar   *result;
  488. Xint     result_len;
  489. Xchar   *str;
  490. Xchar   *daemon;
  491. Xstruct from_host *client;
  492. Xint     pid;
  493. X{
  494. X    char   *end = result + result_len - 1;    /* end of result buffer */
  495. X    char   *expansion;
  496. X    int     expansion_len;
  497. X    char    pid_buf[10];
  498. X
  499. X    /*
  500. X     * %a becomes the client address; %c all user and host information we
  501. X     * have about the client; %d the daemon process name; %h the client host
  502. X     * name or address; %p the daemon process id; %u the remote user name; %%
  503. X     * becomes a %, and %other is ignored. We terminate with a diagnostic if
  504. X     * we would overflow the result buffer.
  505. X     */
  506. X
  507. X    while (*str) {
  508. X    if (*str == '%') {
  509. X        str++;
  510. X        expansion =
  511. X        *str == 'a' ? (str++, client->addr) :
  512. X        *str == 'c' ? (str++, hosts_info(client)) :
  513. X        *str == 'd' ? (str++, daemon) :
  514. X        *str == 'h' ? (str++, FROM_HOST(client)) :
  515. X        *str == 'p' ? (str++, sprintf(pid_buf, "%d", pid), pid_buf) :
  516. X        *str == 'u' ? (str++, client->user) :
  517. X        *str == '%' ? (str++, "%") :
  518. X        *str == 0 ? "" : (str++, "");
  519. X        expansion_len = strlen(expansion);
  520. X    } else {
  521. X        expansion = str++;
  522. X        expansion_len = 1;
  523. X    }
  524. X    if (result + expansion_len >= end) {
  525. X        syslog(LOG_ERR, "shell command too long: %30s...", result);
  526. X        exit(0);
  527. X    }
  528. X    strncpy(result, expansion, expansion_len);
  529. X    result += expansion_len;
  530. X    }
  531. X    *result = 0;
  532. X}
  533. END_OF_FILE
  534.   if test 2134 -ne `wc -c <'percent_x.c'`; then
  535.     echo shar: \"'percent_x.c'\" unpacked with wrong size!
  536.   fi
  537.   # end of 'percent_x.c'
  538. fi
  539. if test -f 'refuse.c' -a "${1}" != "-c" ; then 
  540.   echo shar: Will not clobber existing file \"'refuse.c'\"
  541. else
  542.   echo shar: Extracting \"'refuse.c'\" \(737 characters\)
  543.   sed "s/^X//" >'refuse.c' <<'END_OF_FILE'
  544. X /*
  545. X  * refuse() reports a refused connection, and takes the consequences: in
  546. X  * case of a datagram-oriented service, the unread datagram is taken from
  547. X  * the input queue (or inetd would see the same datagram again and again);
  548. X  * the program is terminated.
  549. X  * 
  550. X  * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
  551. X  */
  552. X
  553. X#ifndef lint
  554. Xstatic char sccsid[] = "@(#) refuse.c 1.2 92/06/11 22:21:34";
  555. X#endif
  556. X
  557. X/* System libraries. */
  558. X
  559. X#include <syslog.h>
  560. X
  561. X/* Local stuff. */
  562. X
  563. X#include "log_tcp.h"
  564. X
  565. X/* refuse - refuse request from bad host */
  566. X
  567. Xvoid    refuse(client)
  568. Xstruct from_host *client;
  569. X{
  570. X    syslog(LOG_WARNING, "refused connect from %s", hosts_info(client));
  571. X    clean_exit(client);
  572. X    /* NOTREACHED */
  573. X}
  574. END_OF_FILE
  575.   if test 737 -ne `wc -c <'refuse.c'`; then
  576.     echo shar: \"'refuse.c'\" unpacked with wrong size!
  577.   fi
  578.   # end of 'refuse.c'
  579. fi
  580. if test -f 'rfc931.c' -a "${1}" != "-c" ; then 
  581.   echo shar: Will not clobber existing file \"'rfc931.c'\"
  582. else
  583.   echo shar: Extracting \"'rfc931.c'\" \(2653 characters\)
  584.   sed "s/^X//" >'rfc931.c' <<'END_OF_FILE'
  585. X /*
  586. X  * rfc931_user() consults the RFC 931 daemon on the client host to look up
  587. X  * the remote user name.
  588. X  * 
  589. X  * Diagnostics are reported through syslog(3).
  590. X  * 
  591. X  * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
  592. X  * 
  593. X  * Inspired by the authutil package (comp.sources.unix volume 22) by Dan
  594. X  * Bernstein (brnstnd@kramden.acf.nyu.edu).
  595. X  */
  596. X
  597. X#ifndef lint
  598. Xstatic char sccsid[] = "@(#) rfc931.c 1.1 92/06/11 22:21:41";
  599. X#endif
  600. X
  601. X#include <stdio.h>
  602. X#include <syslog.h>
  603. X#include <sys/types.h>
  604. X#include <sys/socket.h>
  605. X#include <netinet/in.h>
  606. X#include <setjmp.h>
  607. X#include <signal.h>
  608. X
  609. X#include "log_tcp.h"
  610. X
  611. X#define    RFC931_PORT    113        /* Semi-well-known port */
  612. X#define    TIMEOUT        10        /* wait for at most 10 seconds */
  613. X
  614. Xextern char *strchr();
  615. X
  616. Xstatic jmp_buf timebuf;
  617. X
  618. X/* timeout - handle timeouts */
  619. X
  620. Xstatic void timeout(sig)
  621. Xint     sig;
  622. X{
  623. X    longjmp(timebuf, sig);
  624. X}
  625. X
  626. X/* rfc931_name - return remote user name */
  627. X
  628. Xchar   *rfc931_name(there)
  629. Xstruct sockaddr_in *there;        /* remote link information */
  630. X{
  631. X    struct sockaddr_in here;        /* local link information */
  632. X    struct sockaddr_in sin;        /* for talking to RFC931 daemon */
  633. X    int     length;
  634. X    int     s;
  635. X    unsigned remote;
  636. X    unsigned local;
  637. X    static char user[256];        /* XXX */
  638. X    FILE   *fp;
  639. X    char   *cp;
  640. X    char   *result = FROM_UNKNOWN;
  641. X
  642. X    /* Find out local port number of our stdin. */
  643. X
  644. X    length = sizeof(here);
  645. X    if (getsockname(0, (struct sockaddr *) & here, &length) == -1) {
  646. X    syslog(LOG_ERR, "getsockname: %m");
  647. X    return (result);
  648. X    }
  649. X    /* Set up timer so we won't get stuck. */
  650. X
  651. X    if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  652. X    return (result);
  653. X    signal(SIGALRM, timeout);
  654. X    if (setjmp(timebuf)) {
  655. X    close(s);                /* not: fclose(fp) */
  656. X    return (result);
  657. X    }
  658. X    alarm(TIMEOUT);
  659. X
  660. X    /* Connect to the RFC931 daemon. */
  661. X
  662. X    sin = *there;
  663. X    sin.sin_port = htons(RFC931_PORT);
  664. X    if (connect(s, (struct sockaddr *) & sin, sizeof(sin)) == -1
  665. X    || (fp = fdopen(s, "w+")) == 0) {
  666. X    close(s);
  667. X    return (result);
  668. X    }
  669. X    /* Query the RFC 931 server. Would 13-byte writes ever be broken up? */
  670. X
  671. X    fprintf(fp, "%u,%u\r\n", ntohs(there->sin_port), ntohs(here.sin_port));
  672. X    fflush(fp);
  673. X
  674. X    /* Read response. Kill stdio buffer or we may read back our own query. */
  675. X
  676. X    setbuf(fp, (char *) 0);
  677. X    if (fscanf(fp, "%u , %u : USERID :%*[^:]:%255s", &remote, &local, user) == 3
  678. X    && ferror(fp) == 0 && feof(fp) == 0
  679. X    && ntohs(there->sin_port) == remote
  680. X    && ntohs(here.sin_port) == local) {
  681. X    /* Strip trailing carriage return. */
  682. X
  683. X    if (cp = strchr(user, '\r'))
  684. X        *cp = 0;
  685. X    result = user;
  686. X    }
  687. X    alarm(0);
  688. X    fclose(fp);
  689. X    return (result);
  690. X}
  691. END_OF_FILE
  692.   if test 2653 -ne `wc -c <'rfc931.c'`; then
  693.     echo shar: \"'rfc931.c'\" unpacked with wrong size!
  694.   fi
  695.   # end of 'rfc931.c'
  696. fi
  697. if test -f 'rfc931_option' -a "${1}" != "-c" ; then 
  698.   echo shar: Will not clobber existing file \"'rfc931_option'\"
  699. else
  700.   echo shar: Extracting \"'rfc931_option'\" \(2015 characters\)
  701.   sed "s/^X//" >'rfc931_option' <<'END_OF_FILE'
  702. X*** fromhost.c.orig    Tue Jun  9 19:38:26 1992
  703. X--- fromhost.c    Thu Jun 11 20:39:31 1992
  704. X***************
  705. X*** 90,96 ****
  706. X  int     fromhost(f)
  707. X  struct from_host *f;
  708. X  {
  709. X!     struct sockaddr sa;
  710. X      struct sockaddr_in *sin = (struct sockaddr_in *) (&sa);
  711. X      struct hostent *hp;
  712. X      int     length = sizeof(sa);
  713. X--- 90,96 ----
  714. X  int     fromhost(f)
  715. X  struct from_host *f;
  716. X  {
  717. X!     static struct sockaddr sa;
  718. X      struct sockaddr_in *sin = (struct sockaddr_in *) (&sa);
  719. X      struct hostent *hp;
  720. X      int     length = sizeof(sa);
  721. X***************
  722. X*** 143,156 ****
  723. X      }
  724. X      /* Save the host address. A later inet_ntoa() call may clobber it. */
  725. X  
  726. X      f->addr = strcpy(addr_buf, inet_ntoa(sin->sin_addr));
  727. X- 
  728. X-     /* Look up the remote user name. Does not work for UDP services. */
  729. X- 
  730. X- #ifdef RFC931
  731. X-     if (f->sock_type == FROM_CONNECTED)
  732. X-     f->user = rfc931_name(sin);
  733. X- #endif
  734. X  
  735. X      /* Look up the remote host name. */
  736. X  
  737. X--- 143,150 ----
  738. X      }
  739. X      /* Save the host address. A later inet_ntoa() call may clobber it. */
  740. X  
  741. X+     f->sin = sin;
  742. X      f->addr = strcpy(addr_buf, inet_ntoa(sin->sin_addr));
  743. X  
  744. X      /* Look up the remote host name. */
  745. X  
  746. X*** log_tcp.h.orig    Thu Jun 11 19:16:27 1992
  747. X--- log_tcp.h    Thu Jun 11 20:39:30 1992
  748. X***************
  749. X*** 21,26 ****
  750. X--- 21,27 ----
  751. X      char   *name;            /* host name */
  752. X      char   *addr;            /* host address */
  753. X      char   *user;            /* user name */
  754. X+     struct sockaddr_in *sin;        /* remote link info */
  755. X  };
  756. X  
  757. X  #define FROM_UNKNOWN    "unknown"    /* name or address lookup failed */
  758. X*** options.c.orig    Thu Jun 11 19:15:58 1992
  759. X--- options.c    Thu Jun 11 20:39:31 1992
  760. X***************
  761. X*** 86,91 ****
  762. X--- 86,93 ----
  763. X  
  764. X  /* List of functions that implement the options. Add yours here. */
  765. X  
  766. X+ #define RFC931_OPTION            /* rfc 931 is a run-time option */
  767. X+ 
  768. X  static void user_option();        /* execute "user=name" option */
  769. X  static void group_option();        /* execute "group=name" option */
  770. X  static void twist_option();        /* execute "twist=command" option */
  771. END_OF_FILE
  772.   if test 2015 -ne `wc -c <'rfc931_option'`; then
  773.     echo shar: \"'rfc931_option'\" unpacked with wrong size!
  774.   fi
  775.   # end of 'rfc931_option'
  776. fi
  777. if test -f 'shell_cmd.c' -a "${1}" != "-c" ; then 
  778.   echo shar: Will not clobber existing file \"'shell_cmd.c'\"
  779. else
  780.   echo shar: Extracting \"'shell_cmd.c'\" \(2608 characters\)
  781.   sed "s/^X//" >'shell_cmd.c' <<'END_OF_FILE'
  782. X /*
  783. X  * shell_cmd() takes a shell command template and performs %a (host
  784. X  * address), %c (client info), %h (host name or address), %d (daemon name),
  785. X  * %p (process id) and %u (user name) substitutions. The result is executed
  786. X  * by a /bin/sh child process, with standard input, standard output and
  787. X  * standard error connected to /dev/null.
  788. X  * 
  789. X  * Diagnostics are reported through syslog(3).
  790. X  * 
  791. X  * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
  792. X  */
  793. X
  794. X#ifndef lint
  795. Xstatic char sccsid[] = "@(#) shell_cmd.c 1.2 92/06/11 22:21:28";
  796. X#endif
  797. X
  798. X/* System libraries. */
  799. X
  800. X#include <sys/types.h>
  801. X#include <sys/param.h>
  802. X#include <stdio.h>
  803. X#include <syslog.h>
  804. X
  805. Xextern char *strncpy();
  806. Xextern void closelog();
  807. Xextern void exit();
  808. X
  809. X/* Local stuff. */
  810. X
  811. X#include "log_tcp.h"
  812. X
  813. X/* Forward declarations. */
  814. X
  815. Xstatic void do_child();
  816. X
  817. X/* shell_cmd - expand %<char> sequences and execute shell command */
  818. X
  819. Xvoid    shell_cmd(string, daemon, client)
  820. Xchar   *string;
  821. Xchar   *daemon;
  822. Xstruct from_host *client;
  823. X{
  824. X    char    cmd[BUFSIZ];
  825. X    int     child_pid;
  826. X    int     wait_pid;
  827. X    int     daemon_pid = getpid();
  828. X
  829. X    /*
  830. X     * Most of the work is done within the child process, to minimize the
  831. X     * risk of damage to the parent.
  832. X     */
  833. X
  834. X    switch (child_pid = fork()) {
  835. X    case -1:                    /* error */
  836. X    syslog(LOG_ERR, "fork: %m");
  837. X    break;
  838. X    case 00:                    /* child */
  839. X    percent_x(cmd, sizeof(cmd), string, daemon, client, daemon_pid);
  840. X    do_child(daemon, cmd);
  841. X    /* NOTREACHED */
  842. X    default:                    /* parent */
  843. X    while ((wait_pid = wait((int *) 0)) != -1 && wait_pid != child_pid)
  844. X         /* void */ ;
  845. X    }
  846. X}
  847. X
  848. X/* do_child - exec command with { stdin, stdout, stderr } to /dev/null */
  849. X
  850. Xstatic void do_child(myname, command)
  851. Xchar   *myname;
  852. Xchar   *command;
  853. X{
  854. X    char   *error = 0;
  855. X    int     tmp_fd;
  856. X
  857. X    /*
  858. X     * Close a bunch of file descriptors. The Ultrix inetd only passes stdin,
  859. X     * but other inetd implementations set up stdout as well. Ignore errors.
  860. X     */
  861. X
  862. X    closelog();
  863. X    for (tmp_fd = 0; tmp_fd < 10; tmp_fd++)
  864. X    (void) close(tmp_fd);
  865. X
  866. X    /* Set up new stdin, stdout, stderr, and exec the shell command. */
  867. X
  868. X    if (open("/dev/null", 2) != 0) {
  869. X    error = "open /dev/null: %m";
  870. X    } else if (dup(0) != 1 || dup(0) != 2) {
  871. X    error = "dup: %m";
  872. X    } else {
  873. X    (void) execl("/bin/sh", "sh", "-c", command, (char *) 0);
  874. X    error = "execl /bin/sh: %m";
  875. X    }
  876. X
  877. X    /* We can reach the following code only if there was an error. */
  878. X
  879. X#ifdef LOG_MAIL
  880. X    (void) openlog(myname, LOG_PID, FACILITY);
  881. X#else
  882. X    (void) openlog(myname, LOG_PID);
  883. X#endif
  884. X    syslog(LOG_ERR, error);
  885. X    exit(0);
  886. X}
  887. END_OF_FILE
  888.   if test 2608 -ne `wc -c <'shell_cmd.c'`; then
  889.     echo shar: \"'shell_cmd.c'\" unpacked with wrong size!
  890.   fi
  891.   # end of 'shell_cmd.c'
  892. fi
  893. if test -f 'strcasecmp.c' -a "${1}" != "-c" ; then 
  894.   echo shar: Will not clobber existing file \"'strcasecmp.c'\"
  895. else
  896.   echo shar: Extracting \"'strcasecmp.c'\" \(3767 characters\)
  897.   sed "s/^X//" >'strcasecmp.c' <<'END_OF_FILE'
  898. X/*
  899. X * Copyright (c) 1987 Regents of the University of California.
  900. X * All rights reserved.
  901. X *
  902. X * Redistribution and use in source and binary forms are permitted
  903. X * provided that the above copyright notice and this paragraph are
  904. X * duplicated in all such forms and that any documentation,
  905. X * advertising materials, and other materials related to such
  906. X * distribution and use acknowledge that the software was developed
  907. X * by the University of California, Berkeley.  The name of the
  908. X * University may not be used to endorse or promote products derived
  909. X * from this software without specific prior written permission.
  910. X * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  911. X * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  912. X * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  913. X */
  914. X
  915. X#if defined(LIBC_SCCS) && !defined(lint)
  916. Xstatic char sccsid[] = "@(#)strcasecmp.c    5.6 (Berkeley) 6/27/88";
  917. X#endif /* LIBC_SCCS and not lint */
  918. X
  919. X#include <sys/types.h>
  920. X
  921. X/*
  922. X * This array is designed for mapping upper and lower case letter
  923. X * together for a case independent comparison.  The mappings are
  924. X * based upon ascii character sequences.
  925. X */
  926. Xstatic u_char charmap[] = {
  927. X    '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
  928. X    '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
  929. X    '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
  930. X    '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
  931. X    '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
  932. X    '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
  933. X    '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
  934. X    '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
  935. X    '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
  936. X    '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
  937. X    '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
  938. X    '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
  939. X    '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
  940. X    '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
  941. X    '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
  942. X    '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
  943. X    '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
  944. X    '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
  945. X    '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
  946. X    '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
  947. X    '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
  948. X    '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
  949. X    '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
  950. X    '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
  951. X    '\300', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
  952. X    '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
  953. X    '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
  954. X    '\370', '\371', '\372', '\333', '\334', '\335', '\336', '\337',
  955. X    '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
  956. X    '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
  957. X    '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
  958. X    '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
  959. X};
  960. X
  961. Xstrcasecmp(s1, s2)
  962. X    char *s1, *s2;
  963. X{
  964. X    register u_char    *cm = charmap,
  965. X            *us1 = (u_char *)s1,
  966. X            *us2 = (u_char *)s2;
  967. X
  968. X    while (cm[*us1] == cm[*us2++])
  969. X        if (*us1++ == '\0')
  970. X            return(0);
  971. X    return(cm[*us1] - cm[*--us2]);
  972. X}
  973. X
  974. Xstrncasecmp(s1, s2, n)
  975. X    char *s1, *s2;
  976. X    register int n;
  977. X{
  978. X    register u_char    *cm = charmap,
  979. X            *us1 = (u_char *)s1,
  980. X            *us2 = (u_char *)s2;
  981. X
  982. X    while (--n >= 0 && cm[*us1] == cm[*us2++])
  983. X        if (*us1++ == '\0')
  984. X            return(0);
  985. X    return(n < 0 ? 0 : cm[*us1] - cm[*--us2]);
  986. X}
  987. END_OF_FILE
  988.   if test 3767 -ne `wc -c <'strcasecmp.c'`; then
  989.     echo shar: \"'strcasecmp.c'\" unpacked with wrong size!
  990.   fi
  991.   # end of 'strcasecmp.c'
  992. fi
  993. if test -f 'tcpd.8' -a "${1}" != "-c" ; then 
  994.   echo shar: Will not clobber existing file \"'tcpd.8'\"
  995. else
  996.   echo shar: Extracting \"'tcpd.8'\" \(4928 characters\)
  997.   sed "s/^X//" >'tcpd.8' <<'END_OF_FILE'
  998. X.TH TCPD 8
  999. X.SH NAME
  1000. Xtcpd \- access control facility for internet services
  1001. X.SH SYNOPSIS
  1002. X.B /some/where/tcpd [arguments]
  1003. X.SH DESCRIPTION
  1004. X.PP
  1005. XThe \fItcpd\fR program can be set up to monitor incoming requests for
  1006. X\fItelnet\fR, \fIfinger\fR, \fIftp\fR, \fIexec\fR, \fIrsh\fR,
  1007. X\fIrlogin\fR, \fItftp\fR, \fItalk\fR, \fIspray\fR, \fIrusers\fR,
  1008. X\fIcomsat\fR and other services that have a one-to-one mapping onto
  1009. Xexecutable files.
  1010. X.PP
  1011. XOptional features are:  access control based on pattern matching,
  1012. Xremote username lookups with the RFC 931 protocol, and protection
  1013. Xagainst hosts that pretend to have someone elses host name.
  1014. X.PP
  1015. XRequirements are: network daemons should be launched on-demand, e.g. by
  1016. Xa central process such as the \fIinetd\fR; a syslog(3)-like interface.
  1017. X.SH LOGGING
  1018. XConnections that are monitored by
  1019. X.I tcpd
  1020. Xare reported through the \fIsyslog\fR(3) facility. Each record contains
  1021. Xa time stamp, the remote host name and the name of the service
  1022. Xrequested. The information can be useful to detect unwanted activities,
  1023. Xespecially when logfile information from several hosts is merged.
  1024. X.SH ACCESS CONTROL
  1025. XOptionally,
  1026. X.I tcpd
  1027. Xsupports a simple form of access control that is based on pattern
  1028. Xmatching.  The access-control software provides hooks for the execution
  1029. Xof shell commands when a pattern fires.  For details, see the
  1030. X\fIhosts_access\fR(5) manual page.
  1031. X.SH HOST NAME VERIFICATION
  1032. XThe authentication scheme of some protocols (\fIrlogin, rsh\fR) relies
  1033. Xon host names. Some implementations believe the host name that they get
  1034. Xfrom any random name server; other implementations are more careful but
  1035. Xuse a flawed algorithm.
  1036. X.PP
  1037. X.I tcpd
  1038. Xverifies the remote host name that is returned by the DNS server
  1039. Xresponsible for the address->name mapping, by looking at the host name
  1040. Xand address that are returned by the DNS server responsible for the
  1041. Xname->address mapping. If any discrepancy is detected,
  1042. X.I tcpd
  1043. Xconcludes that it is dealing with a host that pretends to have someone
  1044. Xelses host name.
  1045. X.PP
  1046. XIf the sources are compiled with the \*QPARANOID\*U option,
  1047. X.I tcpd
  1048. Xwill drop the connection in case of a host name/address mismatch.
  1049. XOtherwise,
  1050. X.I tcpd
  1051. Xjust pretends that host name lookup failed when logging the connection
  1052. Xand consulting the optional access control tables.
  1053. X.SH RFC 931
  1054. XWhen RFC 931 lookups are enabled (compile-time option) \fItcpd\fR will
  1055. Xattempt to establish the name of the remote user. This will succeed
  1056. Xonly if the client host runs an RFC 931-compliant daemon.  Remote user
  1057. Xname lookups will not work for datagram-oriented connections, and may
  1058. Xcause noticeable delays in the case of connections from PCs.
  1059. X.SH EXAMPLES
  1060. XThe details of using \fItcpd\fR depend on pathname information that was
  1061. Xcompiled into the program.
  1062. X.SH EXAMPLE 1
  1063. XThis example applies when \fItcpd\fR expects that the original network
  1064. Xdaemons will be moved to a "secret" place.
  1065. X.PP
  1066. XIn order to monitor access to the \fIfinger\fR service, move the
  1067. Xoriginal finger daemon to the "secret" place and install tcpd in the
  1068. Xplace of the original finger daemon. No changes are required to
  1069. Xconfiguration files.
  1070. X.SH EXAMPLE 2
  1071. XThis example applies when \fItcpd\fR expects that the network daemons
  1072. Xare left in their original place.
  1073. X.PP
  1074. XIn order to monitor access to the \fIfinger\fR service, perform the
  1075. Xfollowing edits on the \fIinetd\fR configuration file (usually 
  1076. X\fI/etc/inetd.conf\fR):
  1077. X.nf
  1078. X.sp
  1079. X.ti +5
  1080. Xfinger  stream  tcp  nowait  nobody  /usr/etc/in.fingerd  in.fingerd
  1081. X.sp
  1082. Xbecomes:
  1083. X.sp
  1084. X.ti +5
  1085. Xfinger  stream  tcp  nowait  nobody  /some/where/tcpd     in.fingerd
  1086. X.sp
  1087. X.fi
  1088. X.PP
  1089. XThe example assumes that the network daemons live in /usr/etc.
  1090. X.PP
  1091. XSimilar changes will be needed for the other services that are to be
  1092. Xcovered by \fItcpd\fR.  Send a SIGHUP to the \fIinetd\fR(8) process to
  1093. Xmake the changes effective.
  1094. X.SH EXAMPLE 3
  1095. XIn the case of daemons that do not live in a common directory ("secret"
  1096. Xor otherwise), edit the \fIinetd\fR configuration file so that it
  1097. Xspecifies an absolute path name for the process name field. For example:
  1098. X.nf
  1099. X.sp
  1100. X    ntalk  dgram  udp  wait  root  /some/where/tcpd  /usr/local/lib/ntalkd
  1101. X.sp
  1102. X.fi
  1103. X.PP
  1104. XOnly the last component (ntalkd) of the process name will be used for
  1105. Xaccess control and logging.
  1106. X.SH BUGS
  1107. XSome UDP (and RPC) daemons linger around for a while after they have
  1108. Xfinished their work, in case another request comes in.  In the
  1109. X\fIinetd\fR configuration file these services are registered with the
  1110. X`wait\' option. Only the request that started such a daemon will be
  1111. Xlogged. This restriction does not apply to connection-oriented (TCP)
  1112. Xservices.
  1113. X.SH FILES
  1114. X.PP
  1115. XThe host access control tables are:
  1116. X.PP
  1117. X/etc/hosts.allow
  1118. X.br
  1119. X/etc/hosts.deny
  1120. X.SH SEE ALSO
  1121. Xhosts_access(5), format of the access control tables.
  1122. X.SH AUTHORS
  1123. X.na
  1124. X.nf
  1125. XWietse Venema (wietse@wzv.win.tue.nl),
  1126. XDepartment of Mathematics and Computing Science,
  1127. XEindhoven University of Technology,
  1128. XThe Netherlands.
  1129. X\" @(#) tcpd.8 1.1 92/06/11 22:21:43
  1130. END_OF_FILE
  1131.   if test 4928 -ne `wc -c <'tcpd.8'`; then
  1132.     echo shar: \"'tcpd.8'\" unpacked with wrong size!
  1133.   fi
  1134.   # end of 'tcpd.8'
  1135. fi
  1136. if test -f 'tcpd.c' -a "${1}" != "-c" ; then 
  1137.   echo shar: Will not clobber existing file \"'tcpd.c'\"
  1138. else
  1139.   echo shar: Extracting \"'tcpd.c'\" \(3146 characters\)
  1140.   sed "s/^X//" >'tcpd.c' <<'END_OF_FILE'
  1141. X /*
  1142. X  * General front end for stream and datagram IP services. This program logs
  1143. X  * the remote host name and then invokes the real daemon. For example,
  1144. X  * install as /usr/etc/{tftpd,fingerd,telnetd,ftpd,rlogind,rshd,rexecd},
  1145. X  * after saving the real daemons in the directory "/usr/etc/...". This
  1146. X  * arrangement requires that the network daemons are started by inetd or
  1147. X  * something similar. Connections and diagnostics are logged through
  1148. X  * syslog(3).
  1149. X  * 
  1150. X  * Compile with -DHOSTS_ACCESS in order to enable access control. See the
  1151. X  * hosts_access(5) manual page for details.
  1152. X  * 
  1153. X  * Compile with -DPARANOID if service should be refused to hosts that pretend
  1154. X  * to have someone elses host name. This gives some protection against rsh
  1155. X  * and rlogin attacks that involve compromised domain name servers.
  1156. X  * 
  1157. X  * Compile with -DDAEMON_UMASK=nnn if daemons should run with a non-default
  1158. X  * umask value (the system default is 000, resulting in world-writable
  1159. X  * files).
  1160. X  * 
  1161. X  * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
  1162. X  */
  1163. X
  1164. X#ifndef lint
  1165. Xstatic char sccsid[] = "@(#) tcpd.c 1.3 92/06/11 22:21:22";
  1166. X#endif
  1167. X
  1168. X/* System libraries. */
  1169. X
  1170. X#include <sys/types.h>
  1171. X#include <sys/param.h>
  1172. X#include <sys/stat.h>
  1173. X#include <stdio.h>
  1174. X#include <syslog.h>
  1175. X
  1176. Xextern char *strrchr();
  1177. Xextern char *strcpy();
  1178. X
  1179. X#ifndef MAXPATHNAMELEN
  1180. X#define MAXPATHNAMELEN    BUFSIZ
  1181. X#endif
  1182. X
  1183. X/* Local stuff. */
  1184. X
  1185. X#include "log_tcp.h"
  1186. X
  1187. X/* The following specifies where the vendor-provided daemons should go. */
  1188. X
  1189. X#define REAL_DAEMON_DIR    "/usr/etc/..."
  1190. X
  1191. Xmain(argc, argv)
  1192. Xint     argc;
  1193. Xchar  **argv;
  1194. X{
  1195. X    struct from_host from;
  1196. X    int     from_stat;
  1197. X    char    path[MAXPATHNAMELEN];
  1198. X
  1199. X    /* Attempt to prevent the creation of world-writable files. */
  1200. X
  1201. X#ifdef DAEMON_UMASK
  1202. X    umask(DAEMON_UMASK);
  1203. X#endif
  1204. X
  1205. X    /*
  1206. X     * If argv[0] is an absolute path name, ignore REAL_DAEMON_DIR, and strip
  1207. X     * argv[0] to its basename.
  1208. X     */
  1209. X
  1210. X    if (argv[0][0] == '/') {
  1211. X    strcpy(path, argv[0]);
  1212. X    argv[0] = strrchr(argv[0], '/') + 1;
  1213. X    } else {
  1214. X    sprintf(path, "%s/%s", REAL_DAEMON_DIR, argv[0]);
  1215. X    }
  1216. X
  1217. X    /*
  1218. X     * Open a channel to the syslog daemon. Older versions of openlog()
  1219. X     * require only two arguments.
  1220. X     */
  1221. X
  1222. X#ifdef LOG_MAIL
  1223. X    (void) openlog(argv[0], LOG_PID, FACILITY);
  1224. X#else
  1225. X    (void) openlog(argv[0], LOG_PID);
  1226. X#endif
  1227. X
  1228. X    /*
  1229. X     * Find out and verify the remote host name. Sites concerned with
  1230. X     * security may choose to refuse connections from hosts that pretend to
  1231. X     * have someone elses host name.
  1232. X     */
  1233. X
  1234. X    from_stat = fromhost(&from);
  1235. X#ifdef PARANOID
  1236. X    if (from_stat == -1)
  1237. X    refuse(&from);
  1238. X#endif
  1239. X
  1240. X    /*
  1241. X     * Check whether this host can access the service in argv[0]. The
  1242. X     * access-control code invokes optional shell commands as specified in
  1243. X     * the access-control tables.
  1244. X     */
  1245. X
  1246. X#ifdef HOSTS_ACCESS
  1247. X    if (!hosts_access(argv[0], &from))
  1248. X    refuse(&from);
  1249. X#endif
  1250. X
  1251. X    /* Report remote client and invoke the real daemon program. */
  1252. X
  1253. X    syslog(LOG_INFO, "connect from %s", hosts_info(&from));
  1254. X    (void) execv(path, argv);
  1255. X    syslog(LOG_ERR, "%s: %m", path);
  1256. X    clean_exit(&from);
  1257. X    /* NOTREACHED */
  1258. X}
  1259. END_OF_FILE
  1260.   if test 3146 -ne `wc -c <'tcpd.c'`; then
  1261.     echo shar: \"'tcpd.c'\" unpacked with wrong size!
  1262.   fi
  1263.   # end of 'tcpd.c'
  1264. fi
  1265. if test -f 'try.c' -a "${1}" != "-c" ; then 
  1266.   echo shar: Will not clobber existing file \"'try.c'\"
  1267. else
  1268.   echo shar: Extracting \"'try.c'\" \(2553 characters\)
  1269.   sed "s/^X//" >'try.c' <<'END_OF_FILE'
  1270. X /*
  1271. X  * try - program to try out host access-control tables, including the
  1272. X  * optional shell commands.
  1273. X  * 
  1274. X  * usage: try process_name host_name_or_address
  1275. X  * 
  1276. X  * where process_name is a daemon process name (argv[0] value). If a host name
  1277. X  * is specified, both the name and address will be used to check the address
  1278. X  * control tables. If a host address is specified, the program pretends that
  1279. X  * host name lookup failed.
  1280. X  */
  1281. X
  1282. X#ifndef lint
  1283. Xstatic char sccsid[] = "@(#) try.c 1.2 92/06/11 22:21:32";
  1284. X#endif
  1285. X
  1286. X#include <sys/types.h>
  1287. X#include <netinet/in.h>
  1288. X#include <arpa/inet.h>
  1289. X#include <netdb.h>
  1290. X#include <stdio.h>
  1291. X#include <syslog.h>
  1292. X
  1293. X#ifdef HOSTS_ACCESS
  1294. X
  1295. X#ifndef    INADDR_NONE
  1296. X#define    INADDR_NONE    (-1)        /* XXX should be 0xffffffff */
  1297. X#endif
  1298. X
  1299. X#include "log_tcp.h"
  1300. X
  1301. X/* Try out a (daemon,client) pair */
  1302. X
  1303. Xtry(daemon, name, addr)
  1304. Xchar   *daemon;
  1305. Xchar   *name;
  1306. Xchar   *addr;
  1307. X{
  1308. X    printf("daemon %s: host name %s (address %s): ",
  1309. X       daemon, name, addr);
  1310. X    printf("access %s\n",
  1311. X       hosts_ctl(daemon, name, addr, "you") ? "granted" : "denied");
  1312. X}
  1313. X
  1314. X/* function to intercept the real shell_cmd() */
  1315. X
  1316. Xvoid    shell_cmd(cmd, daemon, client)
  1317. Xchar   *cmd;
  1318. Xchar   *daemon;
  1319. Xstruct from_host *client;
  1320. X{
  1321. X    char    buf[BUFSIZ];
  1322. X    int     pid = getpid();
  1323. X
  1324. X    percent_x(buf, sizeof(buf), cmd, daemon, client, pid);
  1325. X    printf("shell command: %s: ", buf);
  1326. X}
  1327. X
  1328. X/* function to intercept the real process_options() */
  1329. X
  1330. Xprocess_options(options, daemon, client)
  1331. Xchar   *options;
  1332. Xchar   *daemon;
  1333. Xstruct from_host *client;
  1334. X{
  1335. X    char    buf[BUFSIZ];
  1336. X    int     pid = getpid();
  1337. X
  1338. X    percent_x(buf, sizeof(buf), options, daemon, client, pid);
  1339. X    printf("options: %s: ", buf);
  1340. X}
  1341. X
  1342. Xmain(argc, argv)
  1343. Xint     argc;
  1344. Xchar  **argv;
  1345. X{
  1346. X    struct hostent *hp;
  1347. X
  1348. X#ifdef LOG_MAIL
  1349. X    openlog(argv[0], LOG_PID, FACILITY);
  1350. X#else
  1351. X    openlog(argv[0], LOG_PID);
  1352. X#endif
  1353. X
  1354. X    if (argc != 3) {
  1355. X    fprintf(stderr, "usage: %s process_name host_name_or_address\n",
  1356. X        argv[0]);
  1357. X    return (1);
  1358. X    } else {
  1359. X    if (inet_addr(argv[2]) != INADDR_NONE) {/* pretend host name unknown */
  1360. X        try(argv[1], FROM_UNKNOWN, argv[2]);
  1361. X    } else {
  1362. X        if ((hp = gethostbyname(argv[2])) == 0) {    /* bad host name */
  1363. X        fprintf(stderr, "warning: host unknown: %s\n", argv[2]);
  1364. X        try(argv[1], argv[2], "?.?.?.?");
  1365. X        } else {                /* use both name and address */
  1366. X        while (hp->h_addr_list[0])
  1367. X            try(argv[1], hp->h_name,
  1368. X            inet_ntoa(*(struct in_addr *) * hp->h_addr_list++));
  1369. X        }
  1370. X    }
  1371. X    return (0);
  1372. X    }
  1373. X}
  1374. X
  1375. X#else
  1376. X
  1377. Xmain()
  1378. X{
  1379. X    fprintf(stderr, "host access control is not enabled.\n");
  1380. X    return (1);
  1381. X}
  1382. X
  1383. X#endif
  1384. END_OF_FILE
  1385.   if test 2553 -ne `wc -c <'try.c'`; then
  1386.     echo shar: \"'try.c'\" unpacked with wrong size!
  1387.   fi
  1388.   # end of 'try.c'
  1389. fi
  1390. echo shar: End of archive 2 \(of 2\).
  1391. cp /dev/null ark2isdone
  1392. MISSING=""
  1393. for I in 1 2 ; do
  1394.     if test ! -f ark${I}isdone ; then
  1395.     MISSING="${MISSING} ${I}"
  1396.     fi
  1397. done
  1398. if test "${MISSING}" = "" ; then
  1399.     echo You have unpacked both archives.
  1400.     rm -f ark[1-9]isdone
  1401. else
  1402.     echo You still must unpack the following archives:
  1403.     echo "        " ${MISSING}
  1404. fi
  1405. exit 0
  1406. exit 0 # Just in case...
  1407.